//
//  ViewController.m
//  RecordApp
//
//  Created by chaichai on 2022/3/2.
//

#import "ViewController.h"
#import "DataBaseManager.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIDatePicker *picker;
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (NSString *)desText
{
    NSInteger a = [DB totalRecords];
    NSInteger b = [DB totalRecordDays];
    NSInteger c = [DB ceaselessDaysWithDate:self.picker.date];
    NSInteger d = [DB maximumCeaselessDays];
    NSString *msg =
    [NSString stringWithFormat:@"总的打卡次数: %ld\n总的打卡天数: %ld\n选中日期的打卡连续天数:%ld\n历史最高连续打卡天数:%ld",a,b,c,d];
    return msg;
}

- (IBAction)todayAction:(id)sender {
    NSDate *date = self.picker.date;
    NSArray<Record *> *arr = [DB queryRecordsForDay:date];
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"HH:mm:ss"];
    
    NSMutableString *str = [NSMutableString stringWithString:@""];
    [arr enumerateObjectsUsingBlock:^(Record * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [str appendFormat:@"%@\n", [fmt stringFromDate:obj.date]];
    }];
    self.textView.text = str;
}

- (IBAction)addRecord:(id)sender {
    NSDate *date = self.picker.date;
    [DB addRecord:date];
    self.textView.text = self.desText;
}

- (IBAction)removeRecord:(id)sender {
    NSDate *date = self.picker.date;
    [DB removeRecord:date];
    self.textView.text = self.desText;
}

- (IBAction)jumpToList:(id)sender {
    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"list"];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
